package com.xuzao.developer.service.impl;

import com.google.common.collect.Maps;
import com.xuzao.developer.base.model.SysPower;
import com.xuzao.developer.base.model.SysResource;
import com.xuzao.developer.dao.SysPowerDao;
import com.xuzao.developer.dao.SysResourceDao;
import com.xuzao.developer.dto.SysResourceDTO;
import com.xuzao.developer.service.SysResourceService;
import com.xuzao.developer.tools.entity.Pager;
import com.xuzao.developer.vo.BaseResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author xuzao
 * @description
 * @date 2019-09-19 09:38:12
 **/
@Service("sysResourceService")
public class SysResourceServiceImpl implements SysResourceService {

    @Resource
    private SysResourceDao sysResourceDao;
    @Resource
    private SysPowerDao sysPowerDao;

    @Override
    public String list(SysResourceDTO dto, Pager pager) {
        SysPower sysPower = new SysPower();
        sysPower.setUserId(dto.getUserId());
        List<SysPower> sysPowers = sysPowerDao.selectList(sysPower);
        Map<String, Long> powerMap = Maps.newHashMap();
        if (!sysPowers.isEmpty()) {
            sysPowers.forEach(o -> {
                powerMap.put(o.getUrl(), o.getId());
            });
        }
        List<SysResource> sysResources = sysResourceDao.list(dto, pager).getList();
        StringBuilder sb = new StringBuilder();
        if (!sysResources.isEmpty()) {
            int row = 4;
            int col = sysResources.size() / 4 == 0 ? sysResources.size() % 4 : sysResources.size() % 4 + 1;
            int index = 0;
            for (int i = 0; i < col; i++) {
                sb.append(" <tr>");
                for (int j = 0; j < row; j++) {
                    if (index < sysResources.size()) {
                        SysResource sysResource = sysResources.get(index);
                        sb.append("<td>");
                        sb.append("<input type=\"checkbox\" name=\"sysResourceIds\" data-toggle=\"icheck\" data-label=");
                        sb.append(sysResource.getName());
                        if (powerMap.get(sysResource.getUrl()) != null){
                            sb.append(" checked ");
                        }
                        sb.append(" value=\"");

                        sb.append(sysResource.getId()).append("\">");
                        sb.append("</td>");
                        index++;
                    }
                }
                sb.append("</tr>");
            }
        }
        return sb.toString();
    }

    @Override
    public BaseResultVo insert(SysResourceDTO dto) {
        SysResource sysResource = new SysResource();
        //TODO 赋值
        Integer count = sysResourceDao.insertSelective(sysResource);
        if (count > 0) {
            return BaseResultVo.success();
        }
        return BaseResultVo.error();

    }

    @Override
    public BaseResultVo update(SysResourceDTO dto) {
        SysResource sysResource = new SysResource();
        //TODO 赋值
        Integer count = sysResourceDao.updateSelectiveById(sysResource);
        if (count > 0) {
            return BaseResultVo.success();
        }
        return BaseResultVo.error();
    }

    @Override
    public BaseResultVo get(SysResourceDTO dto) {
        SysResource sysResource = new SysResource();
        return BaseResultVo.success(sysResourceDao.selectById(sysResource.getId()));
    }
}